package com.ruirenyun.util.randcode;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 随机生成验证图片
 *
 * @author 张国明 guomingzhang2008@gmail.com <br/>
 *         2012-2-27 下午3:35:03
 *
 */
public class RandCodeImageUtil {

	private static final long serialVersionUID = -1257947018545327308L;
	/**
	 * TODO 同一常量类
	 */
	private static final String SESSION_KEY_OF_RAND_CODE = "randCode";
	/**
	 *
	 */
	private static final int COUNT = 3;

	/**
	 * 定义图形大小
	 */
	private static final int WIDTH = 105;
	/**
	 * 定义图形大小
	 */
	private static final int HEIGHT = 35;
	// private Font mFont = new Font("Arial Black", Font.PLAIN, 15); //设置字体
	/**
	 * 干扰线的长度=1.414*lineWidth
	 */
	private static final int LINE_WIDTH = 5;

	public static Object[] createImage(){
		// 在内存中创建图象
		final BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		// 获取图形上下文
		final Graphics2D graphics = (Graphics2D) image.getGraphics();
		// 设定背景颜色---1
		graphics.setColor(Color.WHITE);
		graphics.fillRect(0, 0, WIDTH, HEIGHT);
		// 设定边框颜色
//		graphics.setColor(getRandColor(100, 200)); // ---2
		graphics.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);

		final Random random = new Random();
		// 随机产生干扰线，使图象中的认证码不易被其它程序探测到
		for (int i = 0; i < COUNT; i++) {
			// ---3
			graphics.setColor(getRandColor(150, 200));
			// 保证画在边框之内
			final int x = random.nextInt(WIDTH - LINE_WIDTH - 1) + 1;
			final int y = random.nextInt(HEIGHT - LINE_WIDTH - 1) + 1;
			final int xl = random.nextInt(500);
			final int yl = random.nextInt(200);
			graphics.setColor(Color.black);
			graphics.drawLine(x, y, x + xl, y + yl);
		}

		// 取随机产生的认证码(4位数字)
		final String resultCode = extractRandCode();
		for (int i = 0; i < resultCode.length(); i++) {
			// 设置字体颜色
			switch (i) {
				case 0:
					graphics.setColor(Color.BLACK);
					break;
				case 1:
					graphics.setColor(Color.blue);
					break;
				case 2:
					graphics.setColor(Color.RED);
					break;
				case 3:
					graphics.setColor(Color.black);
					break;
				default:
					graphics.setColor(Color.GREEN);
			}
			// 设置字体样式
			graphics.setFont(new Font("Times New Roman", Font.ITALIC, HEIGHT - 4));
			// 设置字符，字符间距，上边距
			graphics.drawString(String.valueOf(resultCode.charAt(i)), (23 * i) + 8, 26);
		}
		// 关闭资源
		graphics.dispose();

		//返回验证码和图片
		return new Object[]{resultCode,image};
	}


	/**
	 * @return 随机码
	 */
	public static String extractRandCode() {
		//写死
		final String randCodeType = "5";
		//写死
		int randCodeLength = 4;
		return RandCodeImageEnum.ALL_CHAR.generateStr(randCodeLength);

	}

	/**
	 * @return 手机随机码
	 */
	public static String extractPhoneRandCode() {
		//写死
		final String randCodeType = "5";
		//写死
		int randCodeLength = 6;
		return RandCodeImageEnum.NUMBER_CHAR.generateStr(randCodeLength);

	}

	/**
	 * 描述：
	 *
	 * @param fc
	 *            描述：
	 * @param bc
	 *            描述：
	 *
	 * @return 描述：
	 */
	private static Color getRandColor(int fc, int bc) { // 取得给定范围随机颜色
		final Random random = new Random();
		if (fc > 255) {
			fc = 255;
		}
		if (bc > 255) {
			bc = 255;
		}

		final int r = fc + random.nextInt(bc - fc);
		final int g = fc + random.nextInt(bc - fc);
		final int b = fc + random.nextInt(bc - fc);

		return new Color(r, g, b);
	}
}

